package ch8.multisocket;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class MultiCastSender {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		InetAddress group = InetAddress.getByName("224.0.0.1");
		int port = 4000;
		MulticastSocket ms = null;
		try {
			ms = new MulticastSocket(port);
			while (true) {
				String str = "hello,now is:" + new java.util.Date();
				byte b[] = str.getBytes();
				DatagramPacket dp = new DatagramPacket(b, b.length, group, port);
				ms.send(dp);
				System.out.println("发送到 " + group + ":" + port);
				Thread.sleep(4000);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (ms != null) {
				try {
					ms.leaveGroup(group);
					ms.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
